package com.shop.manager.controller;

//import com.shop.manager.entity.Cities;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.Cities;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.service.CitiesService;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description 城市信息增删改查
 */

@RestController
@RequestMapping("/cities")
public class CitiesController {

    /**
     * @Description 注入service对象
     */
    @Resource(type = CitiesService.class)
    private CitiesService citiesService;

    /**
     * @Description 添加城市信息
     * @param cities 城市对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public HttpResult add(@RequestBody(required = false) @ApiParam(value = "Cities 对象") Cities cities) {
        try {
            citiesService.add(cities);
            return HttpResult.ok("添加城市成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("添加城市失败!");
    }

    /**
     * @Description 批量删除城市信息
     * @param ids 城市id的数组
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public HttpResult delete(@RequestBody Integer[] ids) {
        try {
            citiesService.deleteByIds(ids);
            //删除成功
            return HttpResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //删除异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 修改城市信息
     * @param cities 修改的城市对象
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public HttpResult update(@RequestBody(required = false) @ApiParam(value = "Cities 对象") Cities cities) {
        try {
            citiesService.update(cities);
            return HttpResult.ok("修改城市成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("修改城市失败!");
    }

    /**
     * @Description 根据id获取城市信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public HttpResult getById(@RequestParam("id") Long id) {
        return HttpResult.ok(citiesService.findOne(id));
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param cities 查询条件对象
     * @return
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.POST)
    public HttpResult getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("size") Integer size, @RequestBody(required = false) @ApiParam(value = "Cities 对象") Cities cities) {
        try {
            //分页查询
            PageResult result = citiesService.findPage(pageNum, size, cities);
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error("查询城市异常");
        }
    }

    /**
     * @Description 分页查询
     * @param page 当前页码
     * @param rows 当前页条数
     * @return
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public PageResult findAllAttr(@RequestParam("page") Integer page, @RequestParam("rows") Integer rows) {
        PageResult pageResult = citiesService.findPage(page, rows);
        return pageResult;
    }

    /**
     * @Description 模糊查询城市信息
     * @param cities 城市对象
     * @return
     */
    @RequestMapping(value = "/findByWhere", method = RequestMethod.POST)
    public HttpResult findByWhere(@RequestBody(required = false) @ApiParam(value = "Cities 对象") Cities cities) {
        return HttpResult.ok(citiesService.findByWhere(cities));
    }







}
